#!/usr/bin/python

import elementtree.ElementTree as ET

def indent(elem, level=0):
    i = "\n" + level*"    "
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + "    "
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
        for elem in elem:
            indent(elem, level+1)
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i

def build(M, nx, Seg):
    ts = ['ts', 0]
    annotator = "Average"
    tier_id = "XX"
    a = ['a', 0]

    root = ET.Element("ANNOTATION_DOCUMENT", AUTHOR="", DATE="2010-04-16T10:47:56-06:00", FORMAT="2.6", VERSION="2.6")
    root.attrib["xmlns:xsi"] = "http://www.w3.org/2001/XMLSchema-instance"
    root.attrib["xsi:noNamespaceSchemaLocation"] = "http://www.mpi.nl/tools/elan/EAFv2.6.xsd"

    header = ET.SubElement(root, "HEADER", MEDIA_FILE="", TIME_UNITS="milliseconds")

    media_desc = ET.SubElement(header, "MEDIA_DESCRIPTOR", MEDIA_URL="file:///Users/aslr/Desktop/Andy100307Careful_HF10_Segment1.mov", MIME_TYPE="video/quicktime", RELATIVE_MEDIA_URL="file:/../../../Andy100307Careful_HF10_Segment1.mov")

    prop = ET.SubElement(header, "PROPERTY", NAME="lastUsedAnnotationId")
    prop.text ="642"

    time_order = ET.SubElement(root, "TIME_ORDER")

    tier = ET.SubElement(root, "TIER")
    tier.attrib["ANNOTATOR"] = annotator
    tier.attrib["DEFAUTL_LOCALE"] = "en"
    tier.attrib["LINGUISTIC_TYPE_REF"] = "default-lt"
    tier.attrib["TIER_ID"] = tier_id

    annotation = ET.SubElement(tier, "ANNOTATION")

    linguistic_type = ET.SubElement(root, "LINGUISTIC_TYPE", GRAPHIC_REFERENCES="false", LINGUISTIC_TYPE_ID="default-lt", TIME_ALIGNABLE="true")

    locale = ET.SubElement(root, "LOCALE", COUNTRY_CODE="US", LANGUAGE_CODE="en")

    constraint = ET.SubElement(root, "CONSTRAINT", DESCRIPTION="Time subdivision of parent annotation's time interval, no time gaps allowed within this interval", STEREOTYPE="Time_Subdivision")
    
    constraint = ET.SubElement(root, "CONSTRAINT", DESCRIPTION="Symbolic subdivision of a parent annotation. Annotations refering to the same parent are ordered", STEREOTYPE="Symbolic_Subdivision")

    constraint = ET.SubElement(root, "CONSTRAINT", DESCRIPTION="1-1 association with a parent annotation", STEREOTYPE="Symbolic_Association")

    constraint = ET.SubElement(root, "CONSTRAINT", DESCRIPTION="Time alignable annotations within the parent annotation's time interval, gaps are allowed", STEREOTYPE="Included_In" )

    ## variables:
    ts[1] += 1
    time_slot_id0 = ''.join(str(n) for n in ts)
    time_value0 = str(M)
    time_slot = ET.SubElement(time_order, "TIME_SLOT")
    time_slot.attrib["TIME_SLOT_ID"] = time_slot_id0
    time_slot.attrib["TIME_VALUE"] = time_value0

    ts[1] += 1
    time_slot_id1 = ''.join(str(n) for n in ts)
    time_value1 = str(nx)
    time_slot = ET.SubElement(time_order, "TIME_SLOT")
    time_slot.attrib["TIME_SLOT_ID"] = time_slot_id1
    time_slot.attrib["TIME_VALUE"] = time_value1

    a[1] += 1
    annotation_id = ''.join(str(n) for n in a)
    alignable_annotation = ET.SubElement(annotation, "ALIGNABLE_ANNOTATION")
    alignable_annotation.attrib["ANNOTATION_ID"] = annotation_id
    alignable_annotation.attrib["TIME_SLOT_REF1"] = time_slot_id0
    alignable_annotation.attrib["TIME_SLOT_REF2"] = time_slot_id1

    annot_value = Seg
    annotation_value = ET.SubElement(alignable_annotation, "ANNOTATION_VALUE")
    annotation_value.text = annot_value

    ## output
    indent(root)
    tree = ET.ElementTree(root)
    tree.write("test.eaf", encoding="utf-8")
    return ".eaf file written"
